package MyServlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.Candidate;
import model.Comment;
import model.Department;
import model.Position;
import model.User;
import service.ServiceException;
import ServletService.Service;

/**
 * Servlet implementation class CrudPosition
 */
public class CrudPosition extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public CrudPosition() {
		super();
		// TODO Auto-generated constructor stub
	}

	private String option;
	private Service service;
	private Position position;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		option = request.getParameter("option");

		switch (option) {
		case "add": {
			String name = request.getParameter("positionName");
			String totalPlaces = request.getParameter("totalPlaces");
			String project = request.getParameter("project");
			String requirements = request.getParameter("requirements");
			String department = request.getParameter("department");
			String responsibilities = request.getParameter("responsibilities");
			Department d = Service.getDepartmentService().getDepartmentByName(department);
			Position p = new Position(name, Integer.parseInt(totalPlaces),
					project, requirements, d, responsibilities);
			p.setActive(true);
			Service.getPositionService().insertPosition(p);
			break;
		}
		case "update": {
			Position pos = Service.getPositionService().getById(
					Long.parseLong(request.getParameter("id")));
			response.getWriter().println(pos.getName());
			if (!request.getParameter("name").equals("")) {
				pos.setName(request.getParameter("name"));
			}
			if (!request.getParameter("totalPlaces").equals("")) {
				pos.setTotalPlaces(Integer.parseInt(request
						.getParameter("totalPlaces")));
			}
			if (!request.getParameter("project").equals("")) {
				pos.setProject(request.getParameter("project"));
			}
			if (!request.getParameter("requirements").equals("")) {
				pos.setRequirements(request.getParameter("requirements"));
			}
			if (!request.getParameter("responsibilities").equals("")) {
				pos.setResponsibilities(request
						.getParameter("responsibilities"));
			}
			if (!(request.getParameter("active") == null)) {
				pos.setActive(true);
			} else {
				pos.setActive(false);
			}
			pos.setDepartment(Service.getDepartmentService().getDepartmentByName(request.getParameter("department")));
			
			String[] selectedCand = request.getParameterValues("selectedCand");
			List<Candidate> selCandidates = new ArrayList<Candidate>();
			if(selectedCand != null)
			{
				for(int i = 0; i< selectedCand.length; i++) {
					selCandidates.add(Service.getCandidateService().getCandidateByName(selectedCand[i]));
				}
				pos.setCandidates(selCandidates);
					
			}
			Service.getPositionService().updatePosition(pos);
			break;
		}
		case "delete": {
			Position pos1 = Service.getPositionService().getById(
					Long.parseLong(request.getParameter("id")));
			response.getWriter().println(pos1.getName());
			Service.getPositionService().deletePosition(pos1);
		}
		case "approve": {
			Position pos1 = Service.getPositionService().getById(
					Long.parseLong(request.getParameter("id")));
			response.getWriter().println(pos1.getName());
			pos1.setApproved(true);
			Service.getPositionService().updatePosition(pos1);
			break;
		}
		case "getAll": {
			Service.getPositionService().getAll();
			break;
		}
		case "change": {
			Position p = Service.getPositionService().getPositionByName(
					request.getParameter("department"));
			if (p != null) {
				List<Candidate> candidates = Service.getPositionService()
						.getCandidatesForPosition(p.getId());
				request.setAttribute("candidateList", candidates);
				request.setAttribute("role", "Director de Departament");
				RequestDispatcher rd = request.getRequestDispatcher("/Director de Departament.jsp");
				rd.forward(request, response);
			}
			break;
		}
		case "approveCan":{
			Position p=Service.getPositionService().getById(Long.parseLong(request.getParameter("positionid")));
			String[] selected=request.getParameterValues("active");
			for(String s:selected){
				Candidate c=Service.getCandidateService().getCandidateByID(Long.parseLong(s));
				System.out.println(c.getFirstName());
				if(!c.equals(null)){
					Service.getPositionService().selectCandidate(p.getId(), c);
				}
			}
			for(Candidate c:p.getAcceptedCandidates()){
				System.out.println(c.getFirstName()+"OMG");
			}
		}
		
		}
		RequestDispatcher rd = request.getRequestDispatcher("/login");
		rd.forward(request, response);
	}
}
